package com.teamone.mobilewatch;

import com.teamone.mobilewatch.accessor.webservice.PatientWatchDataAccessorImpl;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

/**
 * Screen to login the app - just a simple imitation of the feature
 * 
 */
public class LoginActivity extends Activity {

	private static final String TAG = "LoginActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// setup button click listeners
		ButtonHandler buttonHandler = new ButtonHandler();
		View btnLogin = findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(buttonHandler);
	}

	/**
	 * 
	 * Class to handle button click events
	 * 
	 */
	class ButtonHandler implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnLogin:
				EditText edtPassword = (EditText) findViewById(R.id.edtPassword);
				EditText edtUserName = (EditText) findViewById(R.id.edtUserName);
				String userName = edtUserName.getText().toString();
				String password = edtPassword.getText().toString();

				// validate fields
				if ("".equals(userName) || "".equals(password)) {
					new AlertDialog.Builder(LoginActivity.this)
							.setMessage(
									"Enter user name and password to login!")
							.setTitle("Error")
							.setNeutralButton(android.R.string.ok,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
										}
									}).show();

				} else {
					// authenticate user
					try {
						
						// temporary disabled
//						boolean authenticated = true;
						boolean authenticated = PatientWatchDataAccessorImpl.getInstance()
								.authenticateUser(userName, password);
						if (authenticated) {
							startActivity(new Intent(getBaseContext(),
									MainMenuActivity.class));
						} else {
							
							
							new AlertDialog.Builder(LoginActivity.this)
							.setMessage(
									"Unable to authenticate user:" + userName)
							.setTitle("Authentication Failed")
							.setNeutralButton(android.R.string.ok,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
										}
									}).show();
							
						}
					} catch (Exception e) {
						Log.d(TAG, e.getMessage());
						new AlertDialog.Builder(LoginActivity.this)
						.setMessage(
								"Server is unavailable.\nPlease try again later...")
						.setTitle("Error")
						.setNeutralButton(android.R.string.ok,
								new DialogInterface.OnClickListener() {
									public void onClick(
											DialogInterface dialog,
											int whichButton) {
									}
								}).show();
					}

				}

				break;
			}
		}

	}

}
